import {
  getCurrentUserInfo
} from '../request/api'
import {
  myGet
} from '../request/index'

// 验证登录有效性
export function verifyToken() {
  return new Promise((resolve) => {
    let headerInfo = wx.getStorageSync('headerInfo');
    if (headerInfo) {
      const userInfo = wx.getStorageSync('userInfo')
      if (!userInfo) {
        resolve(false)
      } else {
        myGet(getCurrentUserInfo, {}, (res) => {
          if (res.status === 200) {
            wx.setStorageSync('userInfo', res.data)
            resolve(res.data)
          } else if (res.status === 401) {
            wx.removeStorageSync('userInfo')
            wx.removeStorageSync('headerInfo')
            resolve(false)
          } else if (res.status === 500) {
            wx.showToast({
              title: res.message,
            })
            resolve(false)
          } else {
            resolve(false)
          }
        })
      }
    } else {
      resolve(false)
    }
  });
}

// 验证是否已更新手机号
export function verifyPhone() {
  const phone = wx.getStorageSync('phone')
  return new Promise((resolve) => {
    if (!phone) {
      resolve(false)
    } else {
      resolve(true)
    }
  })
}